package com.hangzhou.xyj.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.type.EUserType;
import com.hangzhou.xyj.net.handler.CancelFollowHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.CancelFollowReq;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.dialog.LoadingDialog;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.ToastUtil;

import java.util.List;

public class MyFocusAdapter extends BaseLoadingAdapter<UserModel> {
    public MyFocusAdapter(RecyclerView recyclerView, List<UserModel> list) {
        super(recyclerView, list);
    }

    @Override
    public ViewHolder onCreateNormalViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_focus_with_cancel_item, null);
        return new MyFocusViewHolder(view);
    }

    @Override
    public void onBindNormalViewHolder(final ViewHolder holder, final int position) {
        final UserModel userModel = mDataList.get(position);
        ((MyFocusViewHolder) holder).setData(userModel);
        ((MyFocusViewHolder) holder).mTvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TipsDialog(mContext, "提示", "确定取消关注？", "确定", "取消") {
                    @Override
                    protected void OnClickNegativeButton() {
                        this.dismiss();
                    }

                    @Override
                    protected boolean OnClickPositiveButton() {
                        cancelFollow(Integer.valueOf(userModel.getFollowId()), position, holder.getAdapterPosition());
                        this.dismiss();
                        return true;
                    }

                }.show();
            }
        });
        final EUserType userType = PublicUtil.getUserType(userModel.getType(), userModel.getCheckFlag());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userModel.getFollowId().equals(XYJApplication.getUserInfo().getUserId())) {
                    ToastUtil.show("不能点击自己哦~");
                    return;
                }
                IntentManager.getInstance().goExpertInfoActivity(mContext, Integer.valueOf(userModel.getFollowId()),
                        userModel.getNickName(), userType);
            }
        });

    }

    /**
     * 取消关注
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/7 15:27
     */
    private void cancelFollow(final int followId, final int position, final int adapterPosition) {
        final LoadingDialog loadingDialog = new LoadingDialog(mContext);
        CancelFollowReq req = new CancelFollowReq(CancelFollowReq.TYPE_USER, followId, Integer.valueOf(CommonPreferences.getUserId(mContext)));
        HttpLoader.getInstance().sendRequest(new CancelFollowHandler(req, new HttpResponseListener() {
            @Override
            public void onDone(String msg) {

            }

            @Override
            public void onFail(String msg) {
                loadingDialog.dismiss();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onStart() {
                loadingDialog.show();
            }

            @Override
            public void onSuccess(Object model) {
                loadingDialog.dismiss();
                if ((Boolean) model) {
                    //取消关注成功
                    ToastUtil.show("取消关注成功");
                    mDataList.remove(new UserModel(String.valueOf(followId)));
                    notifyItemRemoved(adapterPosition);
                } else {
                    //取消关注失败
                    ToastUtil.show("取消关注失败");
                }
            }
        }, 0));
    }

    private class MyFocusViewHolder extends ViewHolder {
        MyTextView mTvName;
        MyTextView mTvSign;
        CircleImageView mCivHeader;
        MyTextView mTvDelete;//取消关注

        private MyFocusViewHolder(View view) {
            super(view);
            mTvName = (MyTextView) view.findViewById(R.id.mtv_name);
            mTvSign = (MyTextView) view.findViewById(R.id.mtv_sign);
            mCivHeader = (CircleImageView) view.findViewById(R.id.civ_header);
            mTvDelete = (MyTextView) view.findViewById(R.id.mtv_delete);
        }

        public void setData(UserModel user) {
            mTvName.setText(user.getNickName());
            mTvSign.setText(user.getSignature());
            mCivHeader.displayImage(user.getHeadPic());
            //设置昵称右边的认证标志
            EUserType userType = user.getUserType();
            //设置昵称右边的认证标志
            mTvName.setCompoundDrawablesWithIntrinsicBounds(null, null, PublicUtil.getAuthenIconDrawable(mContext, userType), null);
        }
    }
}
